//
//  UITableView+EmptyData.m
//  meets-ios
//
//  Created by 赵鑫哲 on 16/2/25.
//  Copyright © 2016年 meets. All rights reserved.
//

#import "UITableView+EmptyData.h"
#import <objc/runtime.h>

@interface UITableView ()

@property (nonatomic, weak) UILabel     *emptyLabel;

@end

@implementation UITableView (EmptyData)

- (UIImageView *)emptyImageView {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setEmptyImageView:(UIImageView *)emptyImageView {
    objc_setAssociatedObject(self, @selector(emptyImageView), emptyImageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UILabel *)emptyLabel {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setEmptyLabel:(UILabel *)emptyLabel {
    objc_setAssociatedObject(self, @selector(emptyLabel), emptyLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)addEmptyImageAndLabelImage:(UIImage *)image content:(NSString *)content distanceToTop:(CGFloat)distance {
    if (self.emptyImageView || self.emptyLabel) return;
//    WeakSelf(wself);
    UIImageView *emptyImageView = [[UIImageView alloc] initWithImage:image];
    self.emptyImageView = emptyImageView;
    emptyImageView.size = CGSizeMake(ADAPTER_WIDTH_BASED6(115.f), ADAPTER_WIDTH_BASED6(115.f));
    [self addSubview:emptyImageView];
    emptyImageView.x = (self.width - ADAPTER_WIDTH_BASED6(115.f)) / 2.f;
    emptyImageView.y = distance;
    
//    [emptyImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(wself.mas_top).offset(distance);
//        make.centerX.equalTo(wself.mas_centerX);
//        make.size.mas_equalTo(emptyImageView.size);
//    }];
    
    UILabel *emptyLabel = [[UILabel alloc] init];
    self.emptyLabel = emptyLabel;
    [self addSubview:emptyLabel];
    emptyLabel.text = content;
    emptyLabel.textColor = UIColorFromRGB(0x808080);
    emptyLabel.font = [UIFont systemFontOfSize:14];
    emptyLabel.numberOfLines = 0;
    emptyLabel.lineBreakMode = NSLineBreakByWordWrapping;
    emptyLabel.textAlignment = NSTextAlignmentCenter;
    emptyLabel.size = [emptyLabel sizeThatFits:CGSizeMake(self.width - ADAPTER_WIDTH_BASED6(24.f), MAXFLOAT)];
    emptyLabel.x = (self.width - emptyLabel.width) / 2.f;
    emptyLabel.y = CGRectGetMaxY(emptyImageView.frame) + ADAPTER_HEIGHT_BASED6(21.f);
//    emptyLabel.centerX = self.centerX;
//    [emptyLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(emptyImageView.mas_bottom).offset(ADAPTER_HEIGHT_BASED6(21.f));
//        make.centerX.equalTo(wself.mas_centerX);
//        make.size.mas_equalTo(emptyLabel.size);
//    }];
}

- (void)dropEmptyContent {
    RemoveViewIfExist(self.emptyImageView);
    RemoveViewIfExist(self.emptyLabel);
}

@end
